﻿using BaseFramework;
using LiteFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseFramework.Test
{
    public enum SexEnum
    {
        Male = 0,
        Female = 1,
    }

    [Entity("Person", Where = "Id=1")]
    public class Person<T> : PersistentEntity<T>
        where T : class
    {
        public Person()
        {
            this.Id = -1;
        }

        [PrimaryKey("Id", UnsavedValue = "-1", Generator = PrimaryKeyType.Identity)]
        public virtual int Id { get; private set; }

        [Property(Len = 16, MinLen = 4)]
        public string Name { get; set; }

        [Property]
        public SexEnum Sex { get; set; }

        [Property]
        public string Test1 { get; set; }
        [Property]
        public string Test2 { get; set; }
        [Property]
        public string Test3 { get; set; }
        [Property]
        public string Test4 { get; set; }
        [Property]
        public DateTime Test5 { get; set; }
        [Property]
        public string  Test6 { get; set; }
        [Property]
        public decimal Test7 { get; set; }
        [Property]
        public long Test8 { get; set; }
        [Property]
        public byte[] Test9 { get; set; }
        [Property]
        public bool Test10 { get; set; } 

        [Property]
        public int? Age { get; set; }


        [BelongsTo("Kind", BelongsToTable = "MemberType", BelongsToColumn = "MemberType_Id", BelongsToTableAlias = "MemberType")]
        public virtual int UserType { get; set; }
    }

    [Entity("UserInfo", Where = "1=1")]
    public class UserInfo : Person<UserInfo>
    {
        public UserInfo() {
            this.Test11 = DateTime.Now;
        }

        [Property("Name", TableAlias = "MemberType")]
        public string TypeName { get; private set; }



        [DataVersion(AutoVersion = true)]
        public object TimeStamp { get; set; }


        [Property]
        public DateTime Test11 { get; set; }
        //[Property]
        public DateTimeOffset Test12 { get; set; }
        [Property]
        public double Test13 { get; set; }
        [Property]
        public string Test14 { get; set; }
        [Property]
        public Guid Test15 { get; set; }
        [Property]
        public float Test16 { get; set; }
        [Property]
        public byte[] Test17 { get; set; }
        [Property]
        public string Test18 { get; set; }
        [Property]
        public string Test19 { get; set; }
        [Property]
        public string Test20 { get; set; }

        private string _remark = null;
        [Property("Remark")]
        //public string Desc { get { return _remark; } }
        public string Desc { get; set; }



        public override string ToString()
        {
            if (this.TimeStamp is byte[])
                return string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                this.Id,
                this.Name,
                this.Sex.ToString(),
                this.UserType,
                this.TypeName,
                Tools.byteToString((byte[])this.TimeStamp)
                );

            return string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                this.Id,
                this.Name,
                this.Sex.ToString(),
                this.UserType,
                this.TypeName,
                this.TimeStamp
                );
        }
    }

    [Entity]
    class Book : PersistentEntity<Book>
    {
        [PrimaryKey("Id", UnsavedValue = "-1", Generator = PrimaryKeyType.Identity)]
        public virtual int Id { get; set; }

        [Property]
        public string BookName { get; set; }


        [BelongsTo("Holder_Id", BelongsToTable = "UserInfo", BelongsToColumn = "Id", BelongsToTableAlias = "holderAlias")]
        public int Holder_Id { get; set; }



        [BelongsTo("Holder_Id2", BelongsToTable = "UserInfo", BelongsToColumn = "Id", BelongsToTableAlias = "holderAlias2")]
        public int Holder_Id2 { get; set; }



        [BelongsTo("Kind", BelongsToTable = "MemberType", BelongsToColumn = "MemberType_Id", BelongsToTableAlias = "UType", TableAlias = "holderAlias")]
        public int HolderKind { get; set; }


        [Property("Name", TableAlias = "holderAlias")]
        public string Holder_Name { get; set; }

        [Property("Name", TableAlias = "holderAlias2")]
        public string Holder_Name2 { get; set; }


        [Property("Name", TableAlias = "UType")]
        public string UserType { get; set; }
    }

    [Entity]
    class MemberType : PersistentEntity<MemberType>
    {
    }

    public static class ArrayExtension
    {
        public static string ToString(this byte[] buffer)
        {
            return Tools.byteToString(buffer);
        }
    }

    class Tools
    {
        // <summary>
        /// 用于微软的 timestamp 时间戳,比较时传入这个字段进去比较 字节数组转化成字符串 精典 彭定山
        /// </summary>
        /// <param name="byte_">字节数组</param>
        /// <returns>转化成字符串,十六进制</returns>
        public static string byteToString(byte[] byte_)
        {
            // Convert.ToBase64String(byte_);//这个方法是 测试接口的转换方法 网页中默认的测试就是用的这个转化的
            //默认为0的表示方法:0x0000000000000000
            //注意:数据库中的调用比较方法是不加单引号的:SELECT unitcode,unitname,cityid,unitsort,msTimestamp FROM zjytUnitCodeName WHERE msTimestamp>0x0000000000000000
            if (byte_ == null || byte_.Length < 1)
            {
                return "0x0000000000000000";//一定要这样,不然 sql语句的组合变成了 msTimestamp> 就报错了,因为它相当于是个序列性的数字
            }
            else
            {
                string abc5 = "0x" + BitConverter.ToString(byte_).Replace("-", "");
                return abc5;
            }
        }

        public static string ToString(object obj)
        {
            if (obj == null) return "<NULL>";
            if (obj is byte[])
                return byteToString((byte[])obj);
            else
                return obj.ToString();
        }
    }
}